const { DataTypes, Model } = require('sequelize');
const sequelize = require('../config/database');
class Course extends Model { }
// 课程表
Course.init({
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    allowNull: false,
    autoIncrement: true,
    comment: '记录ID'
  },
  name: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '课程名称'
  },
  code: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '课程编码'
  },
  type: {
    comment: '课程类型',
    type: DataTypes.TEXT,
    allowNull: false,
    get () {
      const value = this.getDataValue('type');
      return value ? value.split(',') : [];
    },
    set (value) {
      if (Array.isArray(value)) {
        this.setDataValue('type', value.join(','));
      } else {
        this.setDataValue('type', value);
      }
    },
    validate: {
      notEmpty: true
    }
    // 限制值为 0: 未定义 7: 学科类、1:艺术类 2:技能培训类 9:健身与运动类 10 兴趣爱好 11: 综合类
  },
  class_time: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '上课时间'
  },
  dur_class_time: {
    type: DataTypes.BIGINT,
    allowNull: false,
    comment: '上课时长'
  },
  content: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '上课内容'
  },
  address: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '上课地址'
  },
  stats: {
    comment: '启禁状态',
    type: DataTypes.INTEGER,
    allowNull: false,
    validate: {
      isIn: [[0, 1, 2]] // 限制值为 0: 待启用、1:已启用 2:已禁止
    }
    // defaultValue: 1 // 默认值为 0（未删除）
  },
  ctime: {
    comment: "创建时间",
    type: DataTypes.BIGINT,
    allowNull: false,
    defaultValue: () => new Date().getTime()
  },
  mtime: {
    comment: '更新时间',
    type: DataTypes.BIGINT,
    allowNull: false,
    defaultValue: () => new Date().getTime()
  },
}, {
  sequelize,
  modelName: 'Course'
})
module.exports = Course;